package com.lyshop.jinxiang.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lyshop.jinxiang.R;
import com.lyshop.jinxiang.bean.UserIdCardBean;
import com.lyshop.jinxiang.utils.ToastUtil;


public class ConfirmUserInfoDialog extends MyDialog implements View.OnClickListener {

    private View rootView;
//    private View view_top;
    private RelativeLayout rl_name_bg;
    private EditText et_user_name;
    private TextView tv_user_cert_no;
    private TextView tv_edit;
    private TextView tv_submit;
    private ImageView iv_clear;

    private ConfirmListener mListener;

    public ConfirmUserInfoDialog(Context context, ConfirmListener mListener) {
        this(context, false, Gravity.BOTTOM);
        this.mListener = mListener;
    }

    public ConfirmUserInfoDialog(Context context, boolean isCancelAble, int gravity) {
        super(context, isCancelAble, gravity);
//        Window w = this.getWindow();
//        if (w != null) {
//            w.setWindowAnimations(R.style.dialogTranslateAnim);
//        }
        rootView = LayoutInflater.from(context).inflate(R.layout.dialog_user_info_edit, null);

//        view_top = rootView.findViewById(R.id.view_top);
        tv_submit = rootView.findViewById(R.id.tv_submit);
        rl_name_bg = rootView.findViewById(R.id.rl_name_bg);
        et_user_name = rootView.findViewById(R.id.et_user_name);
        tv_user_cert_no = rootView.findViewById(R.id.tv_user_cert_no);
        tv_edit = rootView.findViewById(R.id.tv_edit);
        iv_clear = rootView.findViewById(R.id.iv_clear);

        setContentView(rootView);

        et_user_name.setEnabled(false);
//        view_top.setOnClickListener(this);
        tv_edit.setOnClickListener(this);
        iv_clear.setOnClickListener(this);
        tv_submit.setOnClickListener(this);

    }

    public void init(UserIdCardBean bean) {
        et_user_name.setText(bean.name);
        tv_user_cert_no.setText(bean.cert_no);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.view_top:
                if (isCancelAble) {
                    dismiss();
                }
                break;
            case R.id.tv_edit:
                showSoftKeyboard();
                rl_name_bg.setBackgroundResource(R.drawable.border_gray_empty_1px);
                break;
            case R.id.iv_clear:
                et_user_name.setText("");
                break;
            case R.id.tv_submit:
                if(TextUtils.isEmpty(et_user_name.getText().toString())){
                    ToastUtil.showToast(mContext,"姓名不能为空");
                    return;
                }
                if(TextUtils.isEmpty(tv_user_cert_no.getText().toString())){
                    ToastUtil.showToast(mContext,"身份证号不能为空");
                    return;
                }
                mListener.onConfirm(et_user_name.getText().toString(),
                        tv_user_cert_no.getText().toString());
                break;
        }
    }

    public void showSoftKeyboard() {
        tv_edit.setVisibility(View.GONE);
        iv_clear.setVisibility(View.VISIBLE);
        et_user_name.setSelection(et_user_name.getText().toString().length());
        et_user_name.setEnabled(true);
        et_user_name.setFocusable(true);
        et_user_name.setFocusableInTouchMode(true);
        et_user_name.requestFocus();
        InputMethodManager inputManager = (InputMethodManager)
                et_user_name.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(et_user_name, 0);
    }

    public interface ConfirmListener {
        void onConfirm(String name,String cert_no);
    }

}

